<?php
namespace app\admin\model;
use think\Model;

class Category extends Model 
{
	protected $field = true;
	protected static function init(){
		
		Category::afterInsert(function($category){
			//接收表单数据
			$categoryData = input('post.');
			
			$categoryId = $category->id;
			
				
			//处理商品推荐位
			if(isset($categoryData['recpos'])){
				foreach($categoryData['recpos'] as $k => $v){
					db('rec_item')->insert(['recpos_id'=>$v,'value_id'=>$categoryId,'value_type'=>2]);
				}
			}
			
		});
		
		Category::beforeUpdate(function ($category) {
			//商品的id
			$categoryId = $category->id;
			//接收表单数据
			$categoryData = input('post.');
			//修改商品属性值
			$attrIds = $categoryData['attr_id'];
			foreach($attrIds as $k => $v){
				if(empty($v)){
					unset($attrIds[$k]);
				}
			}
			if($attrIds){
				$category->search_attr_ids = implode(',',$attrIds);
			}else{
				$category->search_attr_ids ='';
			}
			// dump($categoryData);
		 //   die;
		   db('rec_item')->where(array('value_type'=>2,'value_id'=>$categoryId))->delete();
			if(isset($categoryData['recpos'])){
				foreach($categoryData['recpos'] as $k => $v){
				db('rec_item')->insert(['recpos_id'=>$v,'value_id'=>$categoryId,'value_type'=>2]);
				}
			}	
		  });
	
	Category::beforeInsert(function ($category) {
		// dump($category);die;
		$data = input('post.'); 
		$attrIds = $data['attr_id'];
		foreach($attrIds as $k => $v){
			if(empty($v)){
				unset($attrIds[$k]);
			}
		}
		if($attrIds){
			$category->search_attr_ids = implode(',',$attrIds);
		}
		});
	}
}
